package cn.yunhe.java.controller;


import cn.yunhe.java.dto.AppDTO;
import cn.yunhe.java.service.AppService;
import cn.yunhe.java.service.MerchantService;
import cn.yunhe.java.util.SecurityUtil;
import com.huiminpay.transaction.api.service.PayChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 商户应用接口
 */
@Api(value = "商户平台-应用管理"
        , tags = "商户平台-应用相关"
        , description = "商户平台-应用相关")
@RestController
public class AppController {
    @Reference
    private AppService appService;

    @Reference
    private MerchantService merchantService;

    @Reference
    private PayChannelService payChannelService;

    @ApiOperation("商户创建应用")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "app"
            , value = "应用信息"
            , required = true
            , dataType = "AppDTO"
            , paramType = "body")})
    @PostMapping(value = "/my/apps")
    public AppDTO createApp(@RequestBody AppDTO app) {
        Long merchantId = SecurityUtil.getMerchantId(merchantService);
        return appService.createApp(merchantId, app);
    }

    /**
     *  根据商户id 查询该商户下的应用列表
     */
    @ApiOperation("查询商户下的应用列表")
    @GetMapping(value = "/my/apps")
    public List<AppDTO> queryMyApps() {
        Long merchantId = SecurityUtil.getMerchantId(merchantService);
        return appService.queryAppByMerchant(merchantId);
    }

    /**
     *  根据appId 查询应用详情
     */
    @ApiOperation("根据appid获取应用的详细信息")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "appId"
            , value = "商户应用id"
            , required = true
            , dataType = "String"
            , paramType = "path")})
    @GetMapping(value = "/my/apps/{appId}")
    public AppDTO getApp(@PathVariable String appId) {
        return appService.getAppById(appId);
    }

    @ApiOperation("查询应用是否绑定了某个服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用appId", required = true, dataType =
                    "String", paramType = "query"),
            @ApiImplicitParam(name = "platformChannel", value = "服务类型", required = true, dataType =
                    "String", paramType = "query")
    })
    @GetMapping("/my/merchants/apps/platformchannels")
    public int queryAppBindPlatformChannel(@RequestParam String appId, @RequestParam String
            platformChannel){
        return payChannelService.queryAppBindPlatformChannel(appId,platformChannel);
    }

}
